extends Node2D
class_name ChineseChess

var redScore: int = 0 # 红方得分
var blackScore: int = 0 # 黑方得分
var turn_count: int # 回合数
var current_turn: BoardCommon.TURN # 当前回合
func _ready() -> void:
	GlobalSignals.label_update_change_score_signal.emit(BoardCommon.TURN.RED, redScore)
	GlobalSignals.label_update_change_score_signal.emit(BoardCommon.TURN.BLACK, blackScore)
	current_turn = BoardCommon.TURN.RED
	#接收场景数据
	var game_data = Global.scene_data_manager.get_scene_data("chess_game")
	print(game_data)
#父级处理输入
func _input(event):
	if event is InputEventMouseButton and event.pressed:
		if event.button_index == MOUSE_BUTTON_LEFT:
			var mouse_pos = get_global_mouse_position()
			check_all_areas_at_position(mouse_pos)

func check_all_areas_at_position(pos: Vector2):
	# 获取指定位置的所有 Area2D
	var space_state = get_world_2d().direct_space_state
	var query = PhysicsPointQueryParameters2D.new()
	query.position = pos
	query.collision_mask = 0xFFFFFFFF # 检查所有碰撞层
	query.collide_with_areas = true
	query.collide_with_bodies = false
	
	var results = space_state.intersect_point(query)
	
	for result in results:
		var area = result.collider as Area2D
		if area and area.input_pickable:
			if area.has_method("on_click"):
				area.on_click()

func change_turn() -> void:
	if current_turn == BoardCommon.TURN.RED: current_turn = BoardCommon.TURN.BLACK
	else: current_turn = BoardCommon.TURN.RED
